//
//  ViewController.swift
//  NavigationComb-ib
//
//  Created by BZF on 2020/8/19.
//  Copyright © 2020 bzf. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {
    
    var dictData: NSDictionary!
    var listData: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let plistPath = Bundle.main.path(forResource: "provinces_cities", ofType: "plist")
        
        self.dictData = NSDictionary(contentsOfFile: plistPath!)
      
        let navigationController = self.parent as! UINavigationController
        let selectProvince = navigationController.tabBarItem.title!
        
        print(selectProvince)
        if(selectProvince == "黑龙江"){
            self.listData = self.dictData["黑龙江省"] as! NSArray
            self.navigationItem.title = "黑龙江省信息"
        }else if(selectProvince == "吉林"){
            self.listData = self.dictData["吉林省"] as! NSArray
            self.navigationItem.title = "吉林省信息"
        }else{
            self.listData = self.dictData["辽宁省"] as! NSArray
            self.navigationItem.title = "辽宁省信息"
        }
        
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listData.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
        
        let dict = self.listData[indexPath.row] as! NSDictionary
        
        cell.textLabel?.text = dict["name"] as? String
        
        return cell
        
    }
    
    //场景过渡之前的预处理
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //判断过渡对象的标识符
        if(segue.identifier == "showDetail"){
            let indexPath = self.tableView.indexPathForSelectedRow as! IndexPath
            
            let selectedIndex = indexPath.row
            let dict = self.listData[selectedIndex] as! NSDictionary
        
            //获取要跳转到的视图控制器对象
            let detailViewController = segue.destination as! DetailViewController
            detailViewController.mUrl = dict["url"] as! String
            detailViewController.title = dict["name"] as! String
        }
    }
}

